﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace hcom
{
	/// <summary>
	/// 字符串操作
	/// </summary>
	public class hString
	{

		#region  正则普通替换字符串

		/// <summary>
		///  正则普通替换字符串
		/// </summary>
		/// <param name="src">源字符串</param>
		/// <param name="pattern">要匹配的正则表达式模式</param>
		/// <param name="replacement">替换字符串</param>
		/// <returns>已修改的字符串</returns>
		public string Replace(string src, string pattern, string replacement)
		{
			return Replace(src, pattern, replacement, RegexOptions.None);
		}

		#endregion

		#region 正则替换字符串

		/// <summary>
		///  正则替换字符串

		/// </summary>
		/// <param name="src">要修改的字符串</param>
		/// <param name="pattern">要匹配的正则表达式模式</param>
		/// <param name="replacement">替换字符串</param>
		/// <param name="options">匹配模式</param>
		/// <returns>已修改的字符串</returns>
		public string Replace(string src, string pattern, string replacement, RegexOptions options)
		{
			Regex regex = new Regex(pattern, options | RegexOptions.Compiled);

			return regex.Replace(src, replacement);
		}

		#endregion

		#region 正则不区分大小写替换字符串

		/// <summary>
		/// 正则不区分大小写替换字符串

		/// </summary>
		/// <param name="src">源字符串</param>
		/// <param name="pattern">要匹配的正则表达式模式</param>
		/// <param name="replacement">替换字符串</param>
		/// <returns>已修改的字符串</returns>
		public string ReplaceIgnoreCase(string src, string pattern, string replacement)
		{
			return Replace(src, pattern, replacement, RegexOptions.IgnoreCase);
		}

		#endregion


		/// <summary>
		/// 删除字符串中指定的内容
		/// </summary>
		/// <param name="src">要修改的字符串</param>
		/// <param name="pattern">要删除的正则表达式模式</param>
		/// <returns>已删除指定内容的字符串</returns>
		public string Drop(string src, string pattern)
		{
			return Replace(src, pattern, "");
		}


		/// <summary>
		/// 删除字符串中指定的内容,不区分大小写
		/// </summary>
		/// <param name="src">要修改的字符串</param>
		/// <param name="pattern">要删除的正则表达式模式</param>
		/// <returns>已删除指定内容的字符串</returns>
		public string DropIgnoreCase(string src, string pattern)
		{
			return ReplaceIgnoreCase(src, pattern, "");
		}

		/// <summary>
		/// 替换字符串到数据库可输入模式
		/// </summary>
		/// <param name="src">待插入数据库的字符串</param>
		/// <returns>可插入数据库的字符串</returns>
		public string ToSQL(string src)
		{
			if (src == null)
			{
				return null;
			}
			return Replace(src, "'", "''");
		}

		/// <summary>
		/// 消除前后空格，替换字符串到数据库可输入模式
		/// </summary>
		/// <param name="src">待插入数据库的字符串</param>
		/// <returns>可插入数据库的字符串</returns>
		public string TrimToSQL(string src)
		{
			if (src == null)
			{
				return null;
			}
			return Replace(src.Trim(), "'", "''");
		}

		/// <summary>
		/// 去掉html内容中的指定的html标签
		/// </summary>
		/// <param name="content">html内容</param>
		/// <param name="tagName">html标签</param>
		/// <returns>去掉标签的内容</returns>
		public string DropHtmlTag(string content, string tagName)
		{
			//去掉<tagname>和</tagname>
			return DropIgnoreCase(content, "<[/]{0,1}" + tagName + "[^\\>]*\\>");
		}

		/// <summary>
		/// 去掉html内容中全部标签
		/// </summary>
		/// <param name="content">html内容</param>
		/// <returns>去掉html标签的内容</returns>
		public string DropHtmlTag(string content)
		{
			//去掉<*>
			return Drop(content, "<[^\\>]*>");
		}

		/// <summary>
		/// 判断一个数据是不是数字
		/// </summary>
		/// <param name="inputData">字符串</param>
		/// <returns>结果</returns>
		public bool IsNumeric(string inputData)
		{
			Regex _isNumber = new Regex(@"^\d+");
			Match m = _isNumber.Match(inputData);
			return m.Success;
		}

		/// <summary>
		/// 转换html标签为web页可见内容
		/// </summary>
		/// <param name="src"></param>
		/// <returns></returns>
		public string EscapeHtml(string src)
		{
			if (src == null)
			{
				return null;
			}
			string s = src;
			s = Replace(s, ">", "&gt;");
			s = Replace(s, "<", "&lt;");
			return s;
		}

		/// <summary>
		/// 将字符串格式化成HTML代码
		/// </summary>
		/// <param name="str">要格式化的字符串</param>
		/// <returns>格式化后的字符串</returns>
		public String ToHtml(string str)
		{
			if (str == null || str.Equals(""))
			{
				return str;
			}

			StringBuilder sb = new StringBuilder(str);
			ToHtml( ref sb );
			return sb.ToString();
		}
		public void ToHtml( ref StringBuilder str )
		{
			str.Replace("&", "&amp;");
			str.Replace("<", "&lt;");
			str.Replace(">", "&gt;");
			str.Replace("\t", " ");
			str.Replace("  ", "&nbsp; ");	//替换两个空格，保留一个空格，便于文本中使用html格式<a href="">,标签中只能用一个空格
			str.Replace("\r\n", "<br />");
			str.Replace("\n", "<br />");
			return;
		}

		/// <summary>
		/// 将字符串格式化成HTML代码,回车换行变成p标签
		/// </summary>
		/// <param name="str">要格式化的字符串</param>
		/// <returns>格式化后的字符串</returns>
		public String ToHtmlP(string str)
		{
			if (str == null || str.Equals(""))
			{
				return str;
			}

			StringBuilder sb = new StringBuilder(10000);
			sb.Append(str);
			sb.Replace("&", "&amp;");
			sb.Replace("<", "&lt;");
			sb.Replace(">", "&gt;");
			sb.Replace("\t", " ");
			sb.Replace(" ", "&nbsp;");
			sb.Replace("\r\n", "</p><p>");
			sb.Replace("\n", "</p><p>");
			sb.Insert(0, "<p>");
			sb.Append("</p>");
			return sb.ToString();
		}


		/// <summary>
		/// 将HTML代码转化成文本格式
		/// </summary>
		/// <param name="str">要格式化的字符串</param>
		/// <returns>格式化后的字符串</returns>
		public String ToTxt(String str)
		{
			if (str == null || str.Equals(""))
			{
				return str;
			}

			StringBuilder sb = new StringBuilder(str);
			sb.Replace("&nbsp;", " ");
			sb.Replace("<br />", "\r\n");
			sb.Replace("&lt;", "<");
			sb.Replace("&gt;", ">");
			sb.Replace("&amp;", "&");
			return sb.ToString();
		}

		/// <summary>
		/// 转换成指定的字符串转换成JSON格式
		/// </summary>
		/// <param name="str">in out 源字符串, 返回字符串</param>
		/// <returns>str 变成新字符串</returns>
		public void ToJson(ref StringBuilder str)
		{
			str.Replace("\\", "\\\\");	// 把 \ 换成 \\
			str.Replace("\"", "\\\"");	// 把 " 换成 \"
			str.Replace("\b", "\\b");	// 把 \ 换成 \\
			str.Replace("\f", "\\f");	// 把 \ 换成 \\
			str.Replace("\n", "\\n");	// 把 \ 换成 \\
			str.Replace("\r", "\\r");	// 把 \ 换成 \\
			str.Replace("\t", "\\t");	// 把 \ 换成 \\
		}

		/// <summary>
		/// 去掉HTML中多余的内容
		/// </summary>
		/// <param name="str">输入输出字符串</param>
		public void compressHtml(ref StringBuilder str)
		{
			str.Replace("\r", "");	// 去掉回车符
			str.Replace("\n", "");	// 去掉换行符
			str.Replace("\t", "");	// 去掉制表符
			// ?? 把多个空格换成一个空格
		}

	}
}

